define(['jquery','bbcare',
    'scprit/followup/app/comm/validate.js',
    "scprit/followup/app/comm/attention.js",
    "scprit/followup/app/question/question.servers.js",
    'scprit/patientsQuestionnaire/vm/common.js',
    'scprit/patientsQuestionnaire/vm/patientsLogin.ui.js',
    'scprit/patientsQuestionnaire/vm/patientsLogin.servers.js'],function(jquery,$bb,$validate,$attention,$questionServers,$common,$ui,$servers){
    var login = {};
    //提交函数:
    var showMessage = function(tips){
        $(".message").html(tips);
    };
    //建档回调函数：
    var toBookbuildingCallback = function(data,params){
        if(data.success){
            localStorage.setItem("patientInfo",JSON.stringify({"userId":data.data.patientId,"queueId":params.queueId}))
            window.location.href = 'patientsQuestionnaire.html';
        }
    };
    
    /*
    * 获取问卷回调：
     */
    var getQuestionCallback = function(data,params){
        $(".login_area").css("display",'none');
        $(".input_area").css("display",'block');
        var data = $questionServers.getQuestionData({"list":[data.data.questiones]});//处理的数据格式：{"list":[]}
        
        var question = data.data.questiones[0].children[0];
        $ui.quesInit({"id":"#input_area","data":question});
        $attention.init();
        $('.validatebox').data("attentionFun",$attention.blurInit); // 校验之前提示警告
        $validate.blurInit();
        $("#nextOperate").on("click",function(){
            if($validate.init()){
                toBookbuilding(params)
            }
        })
    }

    //是否建档的回调函数：
    var isBookbuildingCallback = function(data,params){
        if(data.success){
            if(data.data.user == "N"){//未建档
                var questiones = {
		        	"Pub1100001": params.phone,
		        	"Pub1100002":params.username,
//		        	"Pub1100004":"2018-12-10 15:51:00",
		        	"placeOfBirth":"",
		        	"lczsnation":"",
		        	"womenHight":"",
		        	"womenWeight":""
		        };
		        var inParams = {
		            "channel":"BBCARE-APP",
		            "phoneNo":params.phone,
		            "followQueueId":params.queueId,
		            "gender": "F",//女
		            "patientName": params.username,
		            "pregnancyWeek": "",
		            "questiones": questiones,
		            "userType": "2",
		            "opCode": 1000,
		            "opNote": "卵巢早衰评估",
		            "opType": 1
		        };
		        $servers.toBookbuilding(inParams,toBookbuildingCallback,params);
		                
            }else{ //如果已经建档：
                window.location.href = 'patientsQuestionnaire.html';
                localStorage.setItem("patientInfo",JSON.stringify({"userId":data.data.userId,"queueId":params.queueId}));
            }
        }
    };
   //判断是否建档：
    var isBookbuilding = function(params){
        $servers.isBookbuilding(params,isBookbuildingCallback,params);
    };
    //提交：校验验证码：
    var checkVerifyCodeCallback = function(data,params){
        if(!data.success){
            showMessage(data.msg);
        }else{
            showMessage('');
            //校验成功：
            //判断是否建档：
            isBookbuilding(params);
        }
    };
    var checkVerifyCode = function(){
        var login = {};
        login.phone = $.trim($('input[name="phone"]').val());
        login.code = $.trim($('input[name="code"]').val());
        login.username = $.trim($('input[name="username"]').val());
        login.queueId = "12lczsqueue001";
        if(!login.phone||login.phone.length!=11){
            showMessage("请输入11位手机号！");
            $('input[name="phone"]').focus();
            return;
        }
        if(!login.code||login.code.length!=4){
            showMessage("请输入验证码！");
            $('input[name="code"]').focus();
            return;
        }
        if(login.username=="undefined" || login.username==null || login.username==""){
            showMessage("请输入姓名！");
            $('input[name="username"]').focus();
            return;
        }
        $servers.checkVerifyCode(login,checkVerifyCodeCallback,login);
    };

    //获取验证码：
    var getCodeCallback = function(data){
        if(data.success){
            showMessage(data.data.msg);
        }else{
            showMessage(data.msg);
        }
    };
    var getCode = function(){
        var data = {};
        data.phone = $.trim($('input[name="phone"]').val());
        if(!data.phone||data.phone.length!=11){
            showMessage("请输入11位手机号！");
            $('input[name="phone"]').focus();
            return;
        }else{
            //var re = new RegExp("/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[0,1,7])|(18[0-9]))[0-9]{8}$/");
            if(!/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[0,1,7])|(18[0-9]))[0-9]{8}$/.test(data.phone)){
                showMessage("请输入正确的手机号格式！");
                $('input[name="phone"]').focus();
                return;
            }
        }
        //获取：
        $servers.getCode(data,getCodeCallback);
    };

    login.init = function(){
        localStorage.removeItem("patientInfo");
        //样式加载：
        $(".form_login input").off('focus blur');
        $(".form_login").on('focus','input',function(){
            $(this).parents('.form-row').addClass('yellow');
            this.scrollIntoView(true);//使元素弹到祖元素可见区域的顶部
            this.scrollIntoViewIfNeeded();
        });
        $(".form_login").on('blur','input',function(){
            $(this).parents('.form-row').removeClass('yellow');
        });
        $('input[name="phone"]').focus();
        //获取验证码：
        $("#identifyingCode").click(getCode);
        //登录：
        $("#toSubmit").click(checkVerifyCode);
        /*
        * 日期控制不允许填写未来时间：
         */
        $(".questiones").on("input propertychange","input[type='date']",function(){
                var max = $(this).attr("max");
                var value = $(this).val();
                if(max && value){
                    var maxDiff = $common.dateDiff2(value,  max);
                    if(maxDiff*1 > 0){
                        $common.toastShow("不允许填写未来时间",1500);
                        $(this).val('');
                        return;
                    }
                }
            });
    };
    return login;
});
